/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.model;

import nl.xs4all.dylanschell.deckprinter.util.Distance;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SetDescriptor
{
    private final File sourceFile;
    private final UUID id;
    private final String name;

    private final Map<UUID, String> images = new HashMap<UUID, String>();
    private final Map<UUID, CardInSet> cards = new HashMap<UUID, CardInSet>();

    public SetDescriptor(File sourceFile, UUID id, String name)
    {
        this.sourceFile = sourceFile;
        this.id = id;
        this.name = name;
    }

    public UUID getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public void registerImage(UUID uuid, String target)
    {
        images.put(uuid, target);
    }

    public void registerCard(UUID id, CardInSet card)
    {
        cards.put(id, card);
    }

    public String getImage(UUID cardId)
    {
        return images.get(cardId);
    }

    public File getSourceFile()
    {
        return sourceFile;
    }

    public CardInSet getCard(CardInDeck card)
    {
        return cards.get(card.getId());
    }

    public CardInSet getCardByName(String name)
    {
        return getCardByName(name, false);
    }

    public String getImage(CardInSet card)
    {
        return images.get(card.getId());
    }

    public CardInSet getCardById(UUID id)
    {
        return cards.get(id);
    }

    public String getImageById(UUID id)
    {
        return images.get(id);
    }

    public CardInSet getCardByName(String name, boolean fuzzy)
    {
        Distance distance = new Distance();
        int minDistance = Integer.MAX_VALUE;
        CardInSet closestCard = null;
        for (CardInSet cis : cards.values())
        {
            int d = distance.LD(name, cis.getName());
            if (d < minDistance)
            {
                minDistance = d;
                closestCard = cis;
                if (d == 0)
                {
                    break;
                }
            }
        }
        return closestCard;
    }
}
